package com.dong.repeatsubmit.service;

import com.dong.repeatsubmit.entity.AppNameConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

@Service
@Slf4j
public class UserService {
    @Autowired
    @Qualifier("TaskExecutorConfig")
    ThreadPoolTaskExecutor executorService;

    public List<AppNameConfigVO> getTime(){
        List<AppNameConfigVO> appNameConfigVOS = new ArrayList<>();
        //ExecutorService executorService= Executors.newFixedThreadPool(10);
        //Future future = executorService.submit(new changeSkin());
        Future future = executorService.submit(new ChangeSkinService());
        try {
            appNameConfigVOS = (List<AppNameConfigVO>) future.get(1000, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            AppNameConfigVO appNameConfigVO = new AppNameConfigVO();
            appNameConfigVO.setName("默认颜色");
            appNameConfigVO.setThemeId(66);
            appNameConfigVOS.add(appNameConfigVO);
            log.info("执行时间过长了");
        } catch (InterruptedException e) {
            log.info("执行中断异常:{}",e);
        } catch (ExecutionException e) {
            log.info("线程执行异常:{}",e);
        }
        //executorService.shutdown();
        return appNameConfigVOS;
    }

    static class changeSkin implements Callable {
        @Override
        public List<AppNameConfigVO> call() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(300);
            log.info("请求执行时长:{}",System.currentTimeMillis() -start);
            return  new ArrayList();
        }
    }
}
